﻿using Apache.NMS;
using HAArtemis.Interface;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HAArtemis.Entity
{
    public class MqProducer : BaseMq
    {
        ISession session;
        IDestination dest;
        IMessageProducer producer;
        ~MqProducer()
        {
            Connection.CloseAsync();
        }
        /// <summary>
        /// 默认参数方便用户进行软件测试
        /// </summary>
        /// <param name="ipAddress"></param>
        /// <param name="user"></param>
        /// <param name="password"></param>
        public MqProducer(string ipAddress = "amqp://a:a@localhost:5682", string user = "admin", string password = "admin")
        {
            Url = ipAddress;
            Factory = new NMSConnectionFactory(Url);
            Connection = Factory.CreateConnection(user, password);
            Connection.Start();
            session = Connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
        }
        /// <summary>
        /// 创建消费者
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public IMessageProducer CreateProducer(string type = "queue", string name = "ha_message")
        {
            if (type.ToLower() == "queue") dest = session.GetQueue(name);
            else dest = session.GetTopic(name);
            return session.CreateProducer(dest);
        }

        public async Task SendMessage(IMessageProducer sample, string message)
        {
            if (sample == null) return;
            if (string.IsNullOrWhiteSpace(message)) return;
            sample.DeliveryMode = MsgDeliveryMode.NonPersistent;
            await sample.SendAsync(session.CreateTextMessage(message));
        }

        /// <summary>
        /// 发送Queue消息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="content"></param>
        public async Task ProducerQueue(string content, string name = "")
        {
            if (string.IsNullOrWhiteSpace(content)) return;
            if (string.IsNullOrWhiteSpace(name)) name = this.Name;
            if (string.IsNullOrWhiteSpace(name)) return;

            await Task.Run(() =>
            {
                producer.DeliveryMode = MsgDeliveryMode.NonPersistent;
                producer.Send(session.CreateTextMessage(content));
            });
        }
        /// <summary>
        /// 发送Queue消息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="content"></param>
        public async Task ProducerQueue<TEntity>(TEntity content, string name = "")
            where TEntity : IMq, new()
        {
            if (string.IsNullOrWhiteSpace(name)) name = this.Name;
            if (string.IsNullOrWhiteSpace(name)) return;

            await Task.Run(() =>
            {
                ISession session = Connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
                IDestination dest = session.GetQueue(name);
                IMessageProducer producer = session.CreateProducer(dest);
                producer.DeliveryMode = MsgDeliveryMode.NonPersistent;
                producer.Send(session.CreateObjectMessage(content));
            });
        }

        /// <summary>
        /// 发送Topic消息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="content"></param>
        public async Task ProducerTopic(string content, string name = "")
        {
            if (string.IsNullOrWhiteSpace(content)) return;
            if (string.IsNullOrWhiteSpace(name)) name = this.Name;
            if (string.IsNullOrWhiteSpace(name)) return;

            await Task.Run(() =>
            {
                ISession session = Connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
                IDestination dest = session.GetTopic(name);
                IMessageProducer producer = session.CreateProducer(dest);
                producer.DeliveryMode = MsgDeliveryMode.NonPersistent;
                producer.Send(session.CreateTextMessage(content));
            });
        }
        /// <summary>
        /// 发送Topic消息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="content"></param>
        public async Task ProducerTopic<TEntity>(TEntity content, string name = "")
            where TEntity : IMq, new()
        {
            if (string.IsNullOrWhiteSpace(name)) name = this.Name;
            if (string.IsNullOrWhiteSpace(name)) return;

            await Task.Run(() =>
            {
                ISession session = Connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
                IDestination dest = session.GetTopic(name);
                IMessageProducer producer = session.CreateProducer(dest);
                producer.DeliveryMode = MsgDeliveryMode.NonPersistent;
                producer.Send(session.CreateObjectMessage(content));
            });
        }
    }
}
